
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
 
 
public class BallFrame extends JFrame implements Runnable {
	
	public static void main(String [] args)
	{
		BallFrame bball = new BallFrame();
		bball.InitUI();
		Thread t = new Thread(bball);
		t.start();
		
	}
	
	private Graphics g;
	public ArrayList<Ball> list = new ArrayList();
	Random random = new Random();
	public void InitUI()
	{
		
		this.setTitle("弹跳吧小球");
		this.setSize(800, 800);
		this.setResizable(false);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		this.getContentPane().setBackground(Color.black);
		this.setVisible(true);
		g = this.getGraphics();
	
	}
	
	public void paint(Graphics g) {
		//调用父类的重绘方法
		super.paint(g);
		for(int i=1;i<list.size();i++) {
			
			int rrandom = random.nextInt(8);
			ImageIcon IMG = new ImageIcon("C:\\Users\\25280\\Desktop\\CSS之旅\\创萌网_项目创建界面3.0\\images\\subball"+rrandom+".png");
			Image img = IMG.getImage();
			g.drawImage(img, list.get(i).x, list.get(i).y,null);
		}
	}
	public void run() {
			int num=0;//控制小球的生成速度
			while(true) {
				for(int i=0;i<list.size();i++) {
					list.get(i).moveBall();
					repaint();
					list.get(i).isCrash(i);
					if(list.get(i).blood<=0)
					{
						list.remove(i);
					}
				}
				if(num==100) {
					
					int X = random.nextInt(this.getWidth());
					int Y = random.nextInt(this.getHeight());
					int speedX = random.nextInt(5);
					int speedY = random.nextInt(5);
					int b = random.nextInt(10)+40;
					Ball ball=new Ball(X,Y,5,speedX,speedY,g,this,b);
					list.add(ball);
					num=0;
				}
				else num++;
				
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					
					e.printStackTrace();
				}
			}
		}
 
}